package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.AfterLoansService;
import com.woniu.p2p.entity.Invest;
import com.woniu.p2p.entity.InvestAfterloan;
import com.woniu.p2p.vo.InvestAndAfterLoansVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/afterloans")
public class AfterLoansController {

    @Autowired
    AfterLoansService afterLoansService;


    @RequestMapping("/findAllafterloans")
    public String findAllAfterLoan(Map map, String afterloanstatus,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                                   String afterloankeyword,
                                   Integer pagenum,
                                   Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        pagesize=pagesize==null?3:pagesize;
        PageInfo<InvestAndAfterLoansVo> pageInfo = afterLoansService.findAllAfterLoans(pagenum, pagesize, afterloanstatus, predate, nextdate, afterloankeyword);
        map.put("allafterloans",pageInfo);
        map.put("afterloanstatus",afterloanstatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("afterloankeyword",afterloankeyword);
        map.put("pagesize",pagesize);

        List<Invest> allInvest = afterLoansService.findAllInvest();
        map.put("insertInvest",allInvest);
        List<InvestAfterloan> list = afterLoansService.selectAll();
        ArrayList<Integer> ids = new ArrayList<>();
        for(InvestAfterloan investAfterloan :list){
            ids.add(investAfterloan.getAfterloanId());
        }
        map.put("ids",ids);
        return "content/afterLoanManager";
    }


    @RequestMapping("/getinvdetail")
    @ResponseBody
    public Invest getinvdetail(int id){
        Invest allInvestById = afterLoansService.findAllInvestById(id);
        return allInvestById;
    }


    @RequestMapping("/deloneafterloans")
    public int deloneafterloans(int id1,int id2){
        afterLoansService.updateInvestAfterloansId(id2,null);
        afterLoansService.deleteInvestAfterloans(id1);
        return 1;
    }

    //批量删除
    @RequestMapping("/delallafterloans")
    public int delallafterloans(int[] id){
        for (int i = 1; i <=id.length ; i++) {
            afterLoansService.updateInvestAfterloansId(id[i],null);
            Invest allInvestById = afterLoansService.findAllInvestById(id[i]);
            Integer afterloanId = allInvestById.getAfterloanId();
            afterLoansService.deleteInvestAfterloans(afterloanId);
        }
        return 1;
    }

    @RequestMapping("/insertAfterloans")
    public int insertAfterloans(int insertInvest,String afterloanNotice,String afterloanRate,String afterloanCaptial,
                                String afterloanBrate,String afterloanBcaptical){
        InvestAfterloan investAfterloan = new InvestAfterloan(afterloanNotice, afterloanRate, afterloanCaptial, afterloanBrate, afterloanBcaptical);
        afterLoansService.insertAfterloans(investAfterloan);
        int newInsertShuJu = afterLoansService.findNewInsertShuJu();
        afterLoansService.updateInvestAfterloansId(insertInvest,newInsertShuJu);
        return 1;
    }


    /**
     * 导出
     */
    @RequestMapping("/outputafterloans")
    public void exportRepository(int[] ids,
                                 HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");

        List<InvestAndAfterLoansVo> list = afterLoansService.findAllAfterloans(ids);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, InvestAndAfterLoansVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }



}
